/**
 * @author 徐航
 * 红包数据管理
 */
cc.Class({
    extends: cc.Component,
    properties: {
        address_getRedBagConf: "http://game1.szmonster.com/api/user/getRedBagConf",
        address_takeRedBag: "http://game1.szmonster.com/api/user/takeRedBag",
        address_balacne: "http://game1.szmonster.com/api/user/balance",
        address_repayAll: "http://game1.szmonster.com/api/user/repayAll",
        address_adRecordReward: "http://game1.szmonster.com/api/main/report/adRecordReward",
        address_repay: "http://game1.szmonster.com/api/user/repay",
        address_repayRule: "http://game1.szmonster.com/api/user/repayRule",
        address_getStage: "http://game1.szmonster.com/api/main/Repay/getStage",
        address_getPromotionRedBag: "http://game1.szmonster.com/api/user/getPromotionRedBag",
        address_getPromotion: "http://game1.szmonster.com/api/user/getPromotion",
        gridRedBag: '2',
        spellRedBag: '3',
        bigRedBag: '4',
        clockInRedBag: '5',
        passRedBag: 11,
        qiPaoRedBag: 12,
        luckSpinRedBag: 13
    },

    start() {

    },

    /**
     * 请求红包配置
     */
    requestConfigs(callBack) {
        var xhr = cc.loader.getXMLHttpRequest();
        var data = {};
        data['user_id'] = cc.sys.localStorage.getItem('user_id');


        let param = '?'

        for(var key in data){
            var paramStr = key+"="+data[key];
            if(param == ""){
                param += paramStr;
            }else {
                param += "&"+paramStr;
            }
        }
        xhr.open("POST", this.address_getRedBagConf + param);
            xhr.timeout = 5000;//
            // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
            xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status == 200 ) {
                    var respone = xhr.responseText;
                    _registerJson = JSON.parse(respone);
                    if(_registerJson.code == 'success'){
                        console.log('请求红包配置成功', _registerJson);
                        callBack(_registerJson.data);
                    }
                    else {
                        console.log('请求红包配置失败', _registerJson);
                        callBack()
                    }
                    
                    //callBack(JSON.parse(respone));
                }
            };

            xhr.send();
    }
})